package com.visualexpress.uitil;

import java.util.Calendar;
import java.util.Date;
import com.visualexpress.data.Time;

public class DateUitil {
	
	public static Time hourToDate(int hour){
		
		Time d = new Time(0, 0, 0, 0);
		
		d.addTime(hour);
		
		return d;
	}//end of houtToDate
	
	//zxw: write
	//subtract two dates, the result is in hours
	public static double substractInHours(Date date1, Date date2) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date1);
		long time1 = calendar.getTimeInMillis();
		calendar.setTime(date2);
		long time2 = calendar.getTimeInMillis();
		return (double)(time1 - time2) / (1000.0 * 60.0 * 60.0);
	}
	
	public static Date addHours(Date date, int hours) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		long time = calendar.getTimeInMillis();
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTimeInMillis((time + (long)hours * 1000 * 60 * 60));
		return calendar1.getTime();
	}
	
	public static Date addHours(Date date, double hours) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		long time = calendar.getTimeInMillis();
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTimeInMillis((time + (long)(hours * 1000 * 60 * 60)));
		return calendar1.getTime();
	}
	
	public static Date getSystemTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(System.currentTimeMillis());
		return calendar.getTime();
	}
	
	//zxw: write
	//subtract two dates, the result is in days
	public static int substractInDays(Date date1, Date date2) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date1);
		long time1 = calendar.getTimeInMillis();
		calendar.setTime(date2);
		long time2 = calendar.getTimeInMillis();
		return (int)((time1 - time2) / (1000 * 60 * 60 * 24));
	}
	
	public static Date transformToDate(Time time){
		return new Date(time.getYear()-1900,time.getMonth()-1,time.getDay(),time.getHour(),0,0);
	}

}
